//
//  CustomViewPlugin.swift
//  Runner
//
//  Created by longxiaobai on 2024/8/8.
//

import Foundation
import Flutter

class CustomViewPlugin: NSObject, FlutterPlugin {
    static func registerWith (registry:FlutterPluginRegistry) {
        let pluginKey = "com.example/custom_view"
        if (registry.hasPlugin(pluginKey)) {return}
        let registrar = registry.registrar(forPlugin: pluginKey)
        let messenger = registrar?.messenger()
        let factory = NativeFactory(messenger: messenger!)
        registrar!.register(factory, withId: "com.example/custom_view")
    }
    
    static func register(with registrar: FlutterPluginRegistrar) {
        // 注册插件的相关方法通道(MethodChannel)和视图工厂(ViewFactory)
        let channel = FlutterMethodChannel(name: "com.example/custom_channel", binaryMessenger: registrar.messenger())
        
        let instance = CustomViewPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
        
        // 如果需要处理平台视图相关的，还需注册平台视图
        let viewFactory = NativeFactory(messenger: registrar.messenger())
        registrar.register(viewFactory, withId: "com.example/custom_view")
    }
    
    // 实现其他必要的方法
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        print("##### \(call.method) #####")
        // Implementation goes here...
        result(FlutterMethodNotImplemented)
    }
}
